package com.zhiyou.crowdfunding.controller;

import com.zhiyou.crowdfunding.pojo.User;
import com.zhiyou.crowdfunding.service.UserService;
import com.zhiyou.crowdfunding.util.AjaxResult;
import com.zhiyou.crowdfunding.util.Constant;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    @Resource
    public UserService userService;


    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(@Validated User user,String verifyCode, HttpSession session){
        Object verificationCode = session.getAttribute("verificationCode");
        String codeStr=String.valueOf(verificationCode);
        if(!codeStr.equals(verifyCode)){
            return new AjaxResult(false,Constant.VERIFY_CODE_ERROR);  // 验证码错误
        }

       List<User> users= userService.selectByCondition(user);
        if(users.size()==0){
            return new AjaxResult(false,Constant.USER_OR_PASSWORD_ERROR);   // 用户名或密码错误
        }
        User user1=users.get(0);
        session.setAttribute("user",user1);
        return new AjaxResult(true, Constant.LOGIN_SUCCESS);  // 登录成功
    }




}
